<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2020/10/12
 * Time: 4:06 PM
 */
namespace app\admin\logic;

use app\model\Goods;
use app\model\GoodsExchange;
use think\facade\Request;

class IntegralLogic
{

    public function getList()
    {
        $param = Request::get();

        $where = [];
        if(isset($param['name']) || !empty($param['name'])){
            $where[] = ['ge_goods_name','like','%'.trim($param['name']).'%'];
        }

        if(isset($param['pername']) || !empty($param['pername'])){
            $where[] = ['ge_pername','like','%'.trim($param['pername']).'%'];
        }

        $where[] = ['ge_status','=',1];    //默认是 没删除的
        $where[] = ['ge_orgid','=',config('user')['orgid']];
        $goodsExchangeModel = new GoodsExchange();
        $list = $goodsExchangeModel->getGoodsExchangeListByCondition($where);
        return $list;
    }

    public function pass($goodsExchangeId){

        $goodsExchangeModel = new GoodsExchange();
        $goodsExchangeInfo = $goodsExchangeModel->getGoodsExchangeInfoById($goodsExchangeId);

        if(!$goodsExchangeInfo) fail('积分兑换记录不存在');

        if($goodsExchangeInfo['status'] == 0)   fail('积分兑换记录已删除');
        if($goodsExchangeInfo['exchangeStatus'] == 1)   fail('积分兑换记录已兑换成功');

        $goodsExchange = [
            'ge_exchange_status' => 1,
            'ge_update_uid' => config('user.id'),
            'ge_update_name' => config('user.pername'),
        ];
        $rt = $goodsExchangeModel->updateGoodsExchangeInfoByWhere(['ge_id'=>$goodsExchangeId],$goodsExchange);
        if(!$rt)    fail('兑换失败');

        return $rt;
    }

}